﻿using System;
using System.Linq;
using System.Windows;
using ComicQ.Controller;
using ComicQ.Domain.Table;

namespace ComicQ.Dektop.View
{
    /// <summary>
    /// Interaction logic for StaffEditorView.xaml
    /// </summary>
    public partial class StaffEditorView
    {
        private readonly StaffController _staffControl;
        private readonly Staff _staff;

        public StaffEditorView()
        {
            InitializeComponent();
            _staffControl = new StaffController();
            Title = "Add Staff";
        }

        public StaffEditorView(Staff staff)
        {
            InitializeComponent();
            _staffControl = new StaffController();
            Title = "Edit Staff";

            _staff = staff;

            textBoxId.Text = staff.Id.ToString();
            textBoxUsername.Text = staff.Username;
            textBoxPassword.Text = staff.Password;
            textBoxName.Text = staff.Name;
            textBoxAddress.Text = staff.Address;
            textBoxPhone.Text = staff.Phone;
            textBoxEmail.Text = staff.Email;

            textBoxUsername.IsEnabled = false;
        }

        private void ClearTextBox()
        {
            textBoxId.Text = "";
            textBoxUsername.Text = "";
            textBoxPassword.Text = "";
            textBoxName.Text = "";
            textBoxAddress.Text = "";
            textBoxPhone.Text = "";
            textBoxEmail.Text = "";
        }

        private void ButtonSaveClick(object sender, RoutedEventArgs e)
        {
            if (textBoxName.Text.Equals("") || textBoxAddress.Text.Equals("") ||
                textBoxPhone.Text.Equals("") || textBoxEmail.Text.Equals(""))
            {
                MessageBox.Show("field tidak boleh kosong");
            }
            else
            {
                try
                {
                    var phoneDigits = textBoxPhone.Text.ToCharArray();

                    if (phoneDigits.Any(cell => !char.IsDigit(cell)))
                    {
                        throw new Exception();
                    }

                    if (Title.Equals("Add Staff"))
                    {
                        var staff = new Staff
                                        {
                                            Username = textBoxUsername.Text,
                                            Password = textBoxPassword.Text,
                                            Name = textBoxName.Text,
                                            Address = textBoxAddress.Text,
                                            Phone = textBoxPhone.Text,
                                            Email = textBoxEmail.Text,
                                            JoinDate = DateTime.Now
                                        };

                        var message = _staffControl.AddStaff(staff);

                        switch (message)
                        {
                            case 1:
                                MessageBox.Show("sukses menambah staff");
                                ClearTextBox();
                                break;
                            case -1:
                                MessageBox.Show("email sudah dipakai");
                                break;
                            default:
                                MessageBox.Show("username sudah ada");
                                break;
                        }
                    }
                    else
                    {
                        var newStaff = new Staff
                                           {
                                               Id = _staff.Id,
                                               Username = textBoxUsername.Text,
                                               Password = textBoxPassword.Text,
                                               Name = textBoxName.Text,
                                               Address = textBoxAddress.Text,
                                               Email = textBoxEmail.Text,
                                               Phone = textBoxPhone.Text,
                                               JoinDate = _staff.JoinDate
                                           };

                        var message = _staffControl.EditStaff(newStaff, _staff.Email);

                        switch (message)
                        {
                            case 1:
                                MessageBox.Show("sukses mengedit staff");
                                break;
                            default:
                                MessageBox.Show("email sudah dipakai");
                                break;
                        }
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("field phone harus angka");
                }
            }
        }

        private void ButtonCancelClick(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}
